#include <iostream>
#include <algorithm>
#define maxn 1000002
#define inf 0x3fffffff
#define parent(i) (i / 2)
#define lchild(i) (2 * i)
#define rchild(i) (2 * i + 1)
using namespace std;
int heap[maxn], hsize = 0;
void show()
{
    printf("\n此时的堆中元素为:");
    for (int i = 1; i <= hsize; ++i)
    {
        printf(" %d", heap[i]);
    }
}
void tuneHeapUp()
{
    //此时的堆出现局部偏序关系错误,也就是最下层的最右节点不符合堆的偏序关系
    int now = hsize; //最底层最右节点的编号
    int par = parent(now);
    while (now >= 1)
    {
        par = parent(now);
        if (heap[now] >= heap[par] || par == 0)
            break;
        swap(heap[now], heap[par]);
        now = par;
    }
}
void tuneHeapDown()
{
    //此时的堆的顶部元素破坏了堆的偏序关系
    int now = 1; //堆顶
    int l = 0, r = 0;
    while (now <= hsize)
    {
        l = lchild(now);
        r = rchild(now);
        int tmp = heap[l] < heap[r] ? l : r;
        tmp = heap[tmp] < heap[now] ? tmp : now;
        if (tmp == now)
            break;
        swap(heap[tmp], heap[now]);
        now = tmp;
    }
}
void insert(int val) { heap[++hsize] = val, tuneHeapUp(); }
int top()
{
    int val = heap[1];
    if (hsize >= 1)
        heap[1] = heap[hsize], tuneHeapDown();
    heap[hsize--] = inf;
    return val;
}
int main()
{
    fill(heap, heap + maxn, inf);
    for (int i = 0; i <= 10; ++i)
        insert(10 - i), show();
    for (int i = 10; i >= 0; --i)
        printf(" %d", top());
    return 0;
}